<!--
 * @description: 选择镜头
 * @fileName: SelectLens
 * @author: 17076
 * @date: 2025/3/13-下午5:53
 * @version: V1.0.0
-->

<template>
  <cs-dialog
    :dialog="dialogOption"
    @onSubmit="save"
  >
    <div slot="content">
      <el-row :gutter="10">
        <el-col v-for="(item, index) in lensList" :key="index" :span="6">
          <div class="lens-item" :data-ratio="pictureScale" @click.stop="handleLensCheck(item)">
            <el-image :src="downloadUrl + item.imageId" />
            <span class="lens-index">{{ index + 1 }}</span>
            <svg-icon :icon-class="item.checked ? 'checked' : 'uncheck'" class="lens-check"/>
          </div>
        </el-col>
      </el-row>
    </div>
  </cs-dialog>
</template>

<script>
import { queryLensListByFrameId } from '@/api/lens'
import { baseURL } from '@/config'
import { commonDownloadFile } from '@/api/config/uploadApi'
import { mapGetters } from 'vuex'
export default {
  name: 'SelectLens',
  props: {
    // 分镜id
    lensId: {
      type: Number || String,
      default: null
    },
    // 是否是单选
    single: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogOption: {
        title: {
          title: '选择镜头'
        },
        show: false
      },
      downloadUrl: baseURL + commonDownloadFile,
      // 选中的镜头列表
      checkLens: [],
      lensList: []
    }
  },
  computed: {
    // 画面比例
    ...mapGetters({
      pictureScale: 'lens/pictureScale'
    })
  },
  methods: {
    show(checkedLens) {
      this.checkLens = this.$baseLodash.cloneDeep(checkedLens) || []
      this.dialogOption.show = true
      this.fetchLensList()
    },
    // 查询分镜下所有镜头
    fetchLensList() {
      if (!this.lensId) {
        this.$baseMessage.error('请选择一条分镜数据!')
        return
      }
      queryLensListByFrameId({ lensId: this.lensId }).then(res => {
        this.lensList = res.data
        // 存在选中的镜头,将该镜头选中
        if (this.checkLens.length > 0) {
          this.lensList.forEach(item => {
            if (this.checkLens.find(row => row.id === item.id)) {
              this.$set(item, 'checked', true)
            }
          })
        }
      })
    },
    // 镜头选中
    handleLensCheck(item) {
      if (this.single) {
        this.checkLens = []
        this.lensList.forEach(row => {
          this.$set(row, 'checked', false)
        })
        this.$set(item, 'checked', true)
        this.checkLens.push(item)
      } else {
        this.$set(item, 'checked', !item.checked)
        if (item.checked) {
          this.checkLens.push(item)
        } else {
          if (this.checkLens.find(row => row.id === item.id)) {
            this.checkLens = this.checkLens.filter(row => row.id !== item.id)
          }
        }
      }
    },
    save(callback) {
      this.$emit('save', this.checkLens)
      callback(true)
    }
  }
}
</script>

<style scoped lang="scss">
.lens-item {
  position: relative;
  width: 100%;
  max-height: 100%;
  aspect-ratio: $base-aspect-ratio-2;
  margin-bottom: 10px;
  cursor: pointer;
  &[data-ratio="2"] {
    max-width: 100%;
    height: auto;
    aspect-ratio: $base-aspect-ratio-2;
  }
  &[data-ratio="3"] {
    max-width: 100%;
    height: auto;
    aspect-ratio: $base-aspect-ratio-3;
  }
  &[data-ratio="4"] {
    max-height: 100%;
    width: auto;
    aspect-ratio: $base-aspect-ratio-4;
  }
  &[data-ratio="5"] {
    max-height: 100%;
    width: auto;
    aspect-ratio: $base-aspect-ratio-5;
  }
  &[data-ratio="6"] {
    max-height: 100%;
    width: auto;
    aspect-ratio: $base-aspect-ratio-6;
  }
  &[data-ratio="7"] {
    max-height: 100%;
    width: auto;
    aspect-ratio: $base-aspect-ratio-7;
  }
  &[data-ratio="8"] {
    max-height: 100%;
    width: auto;
    aspect-ratio: $base-aspect-ratio-8;
  }
  .el-image {
    width: 100%;
    height: 100%;
    border-radius: 8px;
  }
  .lens-index {
    position: absolute;
    left: 5px;
    top: 5px;
    font-size: 12px;
    background: #FFFFFF;
    border-radius: 3px;
    width: 18px;
    height: 18px;
    line-height: 18px;
    text-align: center;
  }
  .lens-check {
    position: absolute;
    top: 0;
    right: 0;
    width: 22px;
    height: 22px;
  }
}
</style>
